package liang.jiaxin.com.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import liang.jiaxin.com.domain.Customer;
import liang.jiaxin.com.dto.ResponseEntity;
import liang.jiaxin.com.dto.request.customer.CustomerRequestDTO;
import liang.jiaxin.com.dto.response.customer.CustomerResponseDTO;
import liang.jiaxin.com.exception.BusinessException;
import liang.jiaxin.com.mapper.CustomerMapper;

import liang.jiaxin.com.utils.PropertiesUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Jrxsr
 * @since 2019-04-13
 */
@Service
public class CustomerService extends ServiceImpl<CustomerMapper, Customer>{

    @Autowired
    CustomerMapper customerMapper;

    public ResponseEntity login(CustomerRequestDTO requestDTO) {

        QueryWrapper queryWrapper = new QueryWrapper<Customer>();
        queryWrapper.eq(Customer.PHONE, requestDTO.getPhone());
        queryWrapper.eq(Customer.PASSWORD, requestDTO.getPassword());
        Customer customer = customerMapper.selectOne(queryWrapper);
        String token = null;
        if (!Objects.isNull(customer)) {
            token = customer.getToken();
        } else {
            throw new BusinessException("登陆校验失败！");
        }

        ResponseEntity responseEntity = new ResponseEntity();
        CustomerResponseDTO responseDTO = new CustomerResponseDTO();
        BeanUtils.copyProperties(customer, responseDTO);
        responseEntity.setData(responseDTO);
        responseEntity.setMessage("登陆成功！");
        return responseEntity;
    }

    public ResponseEntity create(CustomerRequestDTO requestDTO) {
        ResponseEntity responseEntity = new ResponseEntity();

        Customer customer = new Customer();
        BeanUtils.copyProperties(requestDTO, customer);

        int insert = customerMapper.insert(customer);
        if (insert == 1) {
            responseEntity.setMessage("注册成功！您现在可以登陆啦！");
        } else {
            throw new BusinessException("注册失败，请稍后重试！");
        }
        return responseEntity;
    }

}